"""

Random XMLGenerator for use with Lengte lijnstukken bepalen II

"""

import random, os

q_dir = os.path.join('data', 'questions')

class XMLGenerator():
    def __init__(self, amount):
        self.ax = 0
        self.ay = 0
        self.bx = 0
        self.by = 0
        self.cx = 0
        self.cy = 0
        self.temp = ""
        self.amount = amount
        self.generate_xml()
        
    def generate_random_points(self):
        self.ax = random.randint(1,9)
        self.ay = random.randint(1,9)
        self.bx = random.randint(1,9)
        self.by = random.randint(1,9)
        self.cx = random.randint(1,9)
        self.cy = random.randint(1,9)

    def generate_answers(self):
        self.abx = abs(self.ax - self.bx)
        self.aby = abs(self.ay - self.by)
        self.bcx = abs(self.bx - self.cx)
        self.bcy = abs(self.by - self.cy)
        self.t1answer = ((self.abx*self.abx) + (self.aby*self.aby))
        self.t2answer = ((self.bcx*self.bcx) + (self.bcy*self.bcy))
        
    def generate_xml(self):
        i = 0
        while i < self.amount:
            self.generate_random_points()
            while self.ax == self.bx or self.ay == self.by or self.ax == self.cx or self.ay == self.cy or self.bx == self.cx or self.by == self.cy:
                self.generate_random_points()
            i += 1
            self.generate_answers()
            self.temp += """
<q question="Wat is de totale lengte van de lijnstukken?&#xA;Laat wortels in je antwoord staan. Dus &#8730;___+&#8730;___" answer="&#8730;%d+&#8730;%d">
    <tussenstap stap="Bepaal eerst de lengte van lijnstuk AB" stapanswer="&#8730;%d" />
    <tussenstap stap="Bepaal nu de lengte van lijnstuk BC" stapanswer="&#8730;%d" />
    <tussenstap stap="Tel de lengtes van AB en BC bij elkaar op en je hebt het antwoord" stapanswer="&#8730;%d+&#8730;%d" />
    <figure>
          <point name="A" align="left">%d,%d</point>
          <point name="B" align="right">%d,%d</point>
     </figure>
     <figure>
         <point>%d,%d</point>
         <point name="C" align="top">%d,%d</point>
      </figure>
</q>
            """ % (self.t1answer, self.t2answer, self.t1answer, self.t2answer, self.t1answer, self.t2answer, self.ax, self.ay, self.bx, self.by, self.bx, self.by, self.cx, self.cy)     
            print self.temp
        tempfile = open(os.path.join(q_dir, "tempfile.xml"), "w")
        tempfile.write("<questiondb>" + self.temp + "</questiondb>")
        
def generate_xml_files(amount):
    x = XMLGenerator(amount)
            
if __name__ == "__main__":
    x = XMLGenerator(1)
